﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraBars;
using System.Xml;
using System.Reflection;

namespace Spore.Utils
{
   public class MenuBuilder
    {
       //private static BarManager barmanager;
       public static void GetMenu(BarManager manager,Bar bar, string fileName)
       {
           //barmanager = manager;
           //Bar bar = new Bar();
           //manager.Bars.Add(bar);
           //manager.MainMenu = bar;
          
           XmlDocument doc = new XmlDocument();
           doc.Load(fileName);
           foreach (XmlNode v in doc.DocumentElement.ChildNodes)
           {
               BarItem temp = GetBarItemFromXmlNode(manager,v);
               manager.Items.Add(temp);
               bar.LinksPersistInfo.Add(new LinkPersistInfo(temp));
           }
       }

       private static BarItem GetBarItemFromXmlNode(BarManager manager,XmlNode node)
       {
           if (node.HasChildNodes)
           {
               BarSubItem item = new BarSubItem();
               item.Caption = node.Attributes["text"].Value;
               item.Id = Int32.Parse(node.Attributes["id"].Value);
               item.Name = node.Attributes["name"].Value;
               foreach (XmlNode childNode in node.ChildNodes)
               {
                   BarItem temp = GetBarItemFromXmlNode(manager,childNode);
                   manager.Items.Add(temp);
                   item.LinksPersistInfo.Add(new LinkPersistInfo(temp, childNode.Attributes["beginGroup"].Value == "1"));
               }
               return item;
           }
           else
           {
               BarButtonItem item = new BarButtonItem();
               item.Caption = node.Attributes["text"].Value;
               item.Id = Int32.Parse(node.Attributes["id"].Value);
               item.Name = node.Attributes["name"].Value;
               //item.ItemClick+=(sender,e)=>
               //{
               //    Assembly asmb = Assembly.LoadFrom(node.Attributes["dllFileName"].Value);
               //    Type type = asmb.GetType(node.Attributes["typeName"].Value);
               //    object obj= Activator.CreateInstance(type);
               //    IMenuCommandProcess mcp = obj as IMenuCommandProcess;
               //    if (mcp != null)
               //        mcp.ProcessMenuCommand(item, e);
                   
               //};
               return item;
           }

           
       }
    }
}
